package cn.bonoon.controllers.parsers;

import javax.servlet.http.HttpServletRequest;

import cn.bonoon.kernel.events.PanelEvent;
import cn.bonoon.kernel.menus.ChannelInfo;
import cn.bonoon.kernel.menus.IModuleInfo;
import cn.bonoon.kernel.menus.ModuleManager;
import cn.bonoon.kernel.menus.TabInfo;
import cn.bonoon.kernel.security.LogonUser;
import cn.bonoon.kernel.security.PrivilegeChecker;
import cn.bonoon.kernel.support.services.UserService;
import cn.bonoon.kernel.web.controllers.PanelParser;
import cn.bonoon.kernel.web.models.LayoutModel;

public class TabsPanelParser implements PanelParser{

	protected final ModuleManager moduleManager;
	protected final UserService userService;
	
	public TabsPanelParser(ModuleManager moduleManager, UserService userService) {
		this.moduleManager = moduleManager;
		this.userService = userService;
	}

	@Override
	public PanelEvent parse(String mid, HttpServletRequest request, LogonUser user) throws Exception {
		String home = moduleManager.getEndpoint();
		LayoutModel model = new LayoutModel(mid, request, home);
		TabInfo ti = moduleManager.tab(mid);
//		if(tab instanceof TabInfo){
//			TabInfo ti = (TabInfo)tab;
		PrivilegeChecker privilege= userService.privilege(user, ti);
		
		IModuleInfo module = ti.getModule();
		model.initLayout(ti.navigation(), module);
		
		ChannelInfo channel = ti.getChannel();
		String selectedChannel = channel.getId();
		model.buildChannel(module, user, privilege, selectedChannel);

		model.buildMenu(channel, privilege, ti.getId());
		return new PanelEvent(model, ti, user, privilege);
//		}else if(tab instanceof SimpleTab){
//			SimpleTab st = (SimpleTab)tab;
//			if(ModuleManager.CONFIG_SUPER.equals(st.getKey())){
//				if(!user.isSuper()) throw new Exception("不允许访问该功能！");
//				
//			}else if(ModuleManager.CONFIG_MANAGER.equals(st.getKey())){
//				
//			}else if(ModuleManager.CONFIG_USER.equals(st.getKey())){
//				
//			}
//		}
//		return null;
	}

}
